package com.rz.htk.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.dubbo.service.AdminService;
import com.rz.htk.message.entity.Message;
import com.rz.htk.message.entity.vo.MessageDeleteParam;
import com.rz.htk.message.mapper.MessageMapper;
import com.rz.htk.message.service.IMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Resource
    AdminService adminService;

    @Override
    public IPage<Message> getPage(Message message) {
        QueryWrapper<Message> wrapper = buildWrapper(message);
        wrapper.orderByDesc("id");
        Page<Message> page = new Page<>(message.getPageNo(), message.getPageSize());
        IPage<Message> desktopIPage = page(page, wrapper);
        return desktopIPage;
    }


    private QueryWrapper<Message> buildWrapper(Message message) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        if (message.getType() != null) {
            if (message.getType() == 1) {
                List<Integer> mngID = adminService.getChildrenIds(message.getCrteMngId());
                mngID.add(message.getCrteMngId());
                wrapper.eq("type", message.getType());
                wrapper.in("crte_order_id", mngID);
            } else {
                wrapper.eq("type", message.getType());
            }
        }
        if (StringUtils.isNotEmpty(message.getTitle())) {
            wrapper.like("title", message.getTitle());
        }
        //小于
        if (StringUtils.isNotEmpty(message.getEndTime())) {
            wrapper.lt("creation_time", message.getEndTime());
        }
        //大于
        if (StringUtils.isNotEmpty(message.getStartTime())) {
            wrapper.gt("creation_time", message.getStartTime());
        }
        return wrapper;
    }

    @Override
    @Transactional
    public boolean batchDelete(MessageDeleteParam deleteParam) {
        deleteParam.getIds().forEach(id -> {
            Message message = this.getById(id);
            if (message != null) {
                boolean isOk = this.removeById(message.getId());
                if (!isOk) {
                    throw new GlobalException("消息删除失败！");
                }
            }
        });
        return true;
    }

    public boolean saveMessage(Message message) {
        message.setCreationTime(LocalDateTime.now());
        message.setState(0);
        message.setDelFlag(0);
        boolean isOk = this.saveOrUpdate(message);
        if (!isOk) {
            throw new GlobalException("消息添加失败！");
        }
        return isOk;
    }

}

